package cn.stronglink.assetmanage.business.inspection.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.PatrolDetail;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;

/**
 *
 * 资产列表
 * Created by zhengjinglan on 2018/6/20.
 */

public class PatrolOrderAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> groupNameList;
    private Map<String,List<PatrolDetail>> itemNameList;
    private List<SearchAsset> searchAssets;

    public PatrolOrderAdapter(Context context, List<String> groupNameList) {
        this.context = context;
        this.groupNameList = groupNameList;
        this.itemNameList = new HashMap<>();

        for (int i=0; i<this.groupNameList.size();i++){
            this.itemNameList.put(this.groupNameList.get(i), new ArrayList<PatrolDetail>());
        }
    }


    public void setGroupNameList(List<String> groupNameList) {
        this.groupNameList = groupNameList;
    }

    public void setItemNameList(Map<String,List<PatrolDetail>> itemNameList) {
        this.itemNameList = itemNameList;
    }

    /**
     * 获取组的个数
     *
     * @return
     */
    @Override
    public int getGroupCount() {
        return groupNameList == null ? 0 : groupNameList.size();
    }

    /**
     * 获取指定组中的子元素个数
     *
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        String key = this.groupNameList.get(groupPosition);
        return itemNameList == null ? 0 : itemNameList.get(key).size();
    }

    /**
     * 获取指定组中的数据
     *
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        String key = this.groupNameList.get(groupPosition);
        return groupNameList == null ? null : itemNameList.get(key);
    }

    /**
     * 获取指定组中的指定子元素数据
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key = this.groupNameList.get(groupPosition);
        return itemNameList == null ? null : itemNameList.get(key).get(childPosition);
    }

    /**
     * 获取指定组的ID，这个组ID必须是唯一的
     *
     * @param groupPosition
     * @return
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /**
     * 获取指定组中的指定子元素ID
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 组和子元素是否持有稳定的ID，也就是底层数据的改变不会影响到他们
     *
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_patrol_order,null);
            groupViewHolder = new PatrolOrderAdapter .GroupViewHolder();
            groupViewHolder.tv_patrol_order_name = (TextView)convertView.findViewById(R.id.tv_patrol_order_name);
            groupViewHolder.iv_patrol_open = (ImageView)convertView.findViewById(R.id.iv_patrol_open);

            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (PatrolOrderAdapter.GroupViewHolder)convertView.getTag();
        }
        if(isExpanded){
            groupViewHolder.iv_patrol_open.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.inventory_close));
        }else {
            groupViewHolder.iv_patrol_open.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.inventory_open));
        }
        groupViewHolder.tv_patrol_order_name.setText(groupNameList.get(groupPosition));

        return convertView;
    }

    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据
     *
     * @param groupPosition
     * @param childPosition
     * @param isLastChild
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        PatrolOrderAdapter.ItemViewHolder itemViewHolder = null;
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_inventory_goods,null);
            itemViewHolder = new PatrolOrderAdapter.ItemViewHolder();
            itemViewHolder.tv_asset_name = (TextView)convertView.findViewById(R.id.tv_check_wait_asset_name);
            itemViewHolder.tv_asset_sn = (TextView)convertView.findViewById(R.id.tv_check_wait_sn);
            itemViewHolder.tv_asset_status = (TextView)convertView.findViewById(R.id.tv_check_wait_bind_status);


            TextView tvSn = convertView.findViewById(R.id.tv_check_wait_asset_sn);
            tvSn.setText("RFID");

            convertView.setTag(itemViewHolder);
        }else{
            itemViewHolder = (PatrolOrderAdapter.ItemViewHolder) convertView.getTag();
        }
        String groupName = groupNameList.get(groupPosition);

        PatrolDetail goods = itemNameList.get(groupName).get(childPosition);//获取资产实例
        if (goods.getStatus()== Constants.STATUS_THREE){
            itemViewHolder.tv_asset_status.setText("已巡检");
            itemViewHolder.tv_asset_status.setTextColor(context.getResources().getColor(R.color.bind_green));

        }else {
            itemViewHolder.tv_asset_status.setText("未巡检");
            itemViewHolder.tv_asset_status.setTextColor(context.getResources().getColor(R.color.un_bind_red));
        }


        if (goods.getAssetName() == null || goods.getAssetName().isEmpty()){
            itemViewHolder.tv_asset_status.setText("未知");
        }else {
            itemViewHolder.tv_asset_name.setText(goods.getAssetName());
        }
        itemViewHolder.tv_asset_sn.setText(goods.getRfidNo());

        return convertView;
    }

    /**
     * 是否选中指定位置上的子元素
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        TextView tv_patrol_order_name;
        TextView tv_patrol_child_count;
        ImageView iv_patrol_open;
    }

    class ItemViewHolder {
        TextView tv_asset_name;
        TextView tv_asset_sn;
        TextView tv_asset_status;
        Layout layout_asset_sn;

    }


}

